package com.itheima.utils;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;

/**
 * @author huyoufu <https://github.com/huyoufu>
 * @TIME 2020-08-03 14:47
 * @description
 */
public class SqlSessionFactoryUtil {

    private static SqlSessionFactory sqlSessionFactory;
    static{
        //创建 sqlSessionFactoryBuilder
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();


        InputStream inputStream = SqlSessionFactoryUtil.class.getClassLoader().getResourceAsStream("SqlMapConfig.xml");

        //使用build创建sqlSessionFactory
        sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);

    }

    public static SqlSessionFactory getSessionFactory(){
        return sqlSessionFactory;
    }
    public static SqlSession getSqlSession(boolean autocommit){
        return sqlSessionFactory.openSession(autocommit);
    }
}
